#include "ChangingStateTimer.h"

#include "../SimpleDockWindow.h"

ChangingStateTimer::ChangingStateTimer(MainComponent * mainComponent_) : mainComponent (mainComponent_),
											startTime (-1),
											changingState (false),
											hidden(true),
											shouldBeHidden(true),
											mouseInside(false),
											animator (0)
{
	animator = new SimpleDockAnimator(mainComponent);
}

ChangingStateTimer::~ChangingStateTimer()
{
	if(animator)
		delete animator;
}

void ChangingStateTimer::timerCallback()
{
	if(shouldBeHidden)
	{
		if(!changingState)
		{
			if(hidden)
			{
				if(mouseInside)
				{
					if(startTime == -1)
						startTime = Time::getMillisecondCounter();

					if(Time::getMillisecondCounter() - startTime > 400)
					{
//						const MessageManagerLock mmLock;

						changingState = true;

						animator->showWindow();

					}
				}
				else
				{
					startTime = -1;
					stopTimer();
				}
			} else
			{
				if(!mouseInside)
				{
					if(startTime == -1)
						startTime = Time::getMillisecondCounter();

					if(Time::getMillisecondCounter() - startTime > 1000)
					{
			//			const MessageManagerLock mmLock;

						changingState = true;

						animator->hideWindow();
					}
				}
				else
				{
					startTime = -1;
					stopTimer();
				}
			}
		} else
		{
			//is already changing state
			if(!animator->isMoving())
			{
				changingState = false;

				hidden = !hidden;

				startTime = -1;

				stopTimer();
			}
		}
	}
}

void ChangingStateTimer::mouseIn()
{
	mouseInside = true;

	if(hidden && !changingState)
	{
		if(!isTimerRunning())
			startTimer(50);
	}
}

void ChangingStateTimer::mouseOut()
{
	mouseInside = false;

	if(!hidden && !changingState)
	{
		if(!isTimerRunning())
			startTimer(50);
	}
}